<template>
	<el-dialog v-model="isVisible" title="集市规则配置" width="500px" :before-close="handleClose">
		<el-form ref="formRef" class="good-service-form" :model="form" :rules="rules" label-width="80px" inline>
			<el-form-item label="集市规则" prop="rule">
				<el-input type="textarea" v-model="form.rule" :maxlength="1000" :show-word-limit="true" rows="10" />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">取消</el-button>
				<el-button type="primary" v-debounce="submit">保存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { FormRules, ElMessage } from 'element-plus';
import { ref, reactive, watch } from 'vue';
import { fetchMarketRuleInfo, fetchMarketRule } from '/@/api/liquorCer';

const props = defineProps({
	// 列表内容
	visible: {
		type: Boolean,
		default: () => false,
	},
	row: {
		type: Object,
		default: () => null,
	},
});

const emit = defineEmits(['handleClose']);

const isVisible = ref(false);

const formRef = ref();
const form = ref<any>({});

//校验规则
const rules = reactive<FormRules<any>>({
	rule: [{ required: true, trigger: 'blur', message: '集市规则不能为空' }],
});

watch(
	() => props.visible,
	(val) => {
		isVisible.value = val;
		if (val) {
			getRule();
		}
	},
	{
		immediate: true,
	}
);

const submit = async () => {
	formRef.value.validate(async (valid: boolean) => {
		if (valid) {
			const res = await fetchMarketRule({ ...form.value });
			if (res.code == 200) {
				ElMessage.success('配置完成');
				handleClose(true);
			} else {
				ElMessage.error(res.msg);
			}
		}
	});
};

const handleClose = (success?: boolean) => {
	form.value = {};
	formRef.value.resetFields();
	emit('handleClose', success === true);
};

const getRule = async () => {
	const res = await fetchMarketRuleInfo();
	if (res.code == 200) {
		form.value = res.data;
	}
};
</script>

<style lang="scss">
.good-service-form {
	.el-form-item {
		width: 90%;
		.el-input-number {
			width: 100%;
		}
	}
}
</style>
